﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JobsBoardDAL.DTOs;
using JobsBoardDAL;
using System.Windows;

namespace JobBoardAdminPanel.ViewModels
{
    public class OfferViewModel : ViewModelBase
    {
        JobOfferDTO offer;

        public OfferViewModel(JobOfferDTO offer)
        {
            this.offer = offer;
            if (offer.Industry == null)
            {
                this.offer.Industry = new IndustryDTO();

            }
            if (offer.Location == null)
            {
                this.offer.Location = new LocationDTO();
            }

        }

        public int OfferID 
        {
            get
            {
                return this.offer.ID;
            }
        }

        public string Status
        {
            get
            {
                if (this.offer.Status == null)
                {
                    this.offer.Status = new StatusDTO();
                }

                return this.offer.Status.StatusName;
            }
            set
            {
                if (this.offer.Status == null)
                {
                    this.offer.Status = new StatusDTO();
                }

                if (this.offer.Status.StatusName != value)
                {
                    this.offer.Status.StatusName = value;
                    OnPropertyChanged("Status");
                }
            }
        }

        public DateTime PublishDate
        {
            get
            {
                return this.offer.PublishTime;
            }
            set
            {
                if (this.offer.PublishTime != value)
                {
                    this.offer.PublishTime = value;
                    OnPropertyChanged("PublishDate");
                }
            }
        }

        public int ValidDaysRemaining
        {
            get
            {
                int num = (this.PublishDate.Day + this.offer.ValidityDays) - DateTime.Now.Day + 2;
                if(num < 0) num = 0;

                return num;
            }
            set
            {
                if (this.offer.ValidityDays != value)
                {
                    this.offer.ValidityDays = value;
                    OnPropertyChanged("ValidDaysRemaining");
                }
            }
        }

        public IndustryViewModel Industry
        {
            get
            {
                return new IndustryViewModel(this.offer.Industry);
            }
            set
            {
                if (this.offer.Industry == null)
                {
                    this.offer.Industry = new IndustryDTO();
                }

                this.offer.Industry.IndustryName = value.IndustryName;
                OnPropertyChanged("Industry");
            }
        }

        public string City
        {
            get
            {
                return this.offer.Location.Town.CityName;
            }
            set
            {
                if (this.offer.Location.Town.CityName != value)
                {
                    this.offer.Location.Town.CityName = value;
                    OnPropertyChanged("City");
                }
            }
        }

        public string Country
        {
            get
            {
                return this.offer.Location.Country.CountryName;
            }
            set
            {
                if (this.offer.Location.Country.CountryName != value)
                {
                    this.offer.Location.Country.CountryName = value;
                    OnPropertyChanged("Country");
                }
            }
        }

        public string JobTitle
        {
            get
            {
                return this.offer.JobTitle;
            }
            set
            {
                if (this.offer.JobTitle != value)
                {
                    this.offer.JobTitle = value;
                    OnPropertyChanged("JobTitle");
                }
            }
        }

        public string JobDescription
        {
            get
            {
                return this.offer.JobDescription;
            }
            set
            {
                if(this.offer
                .JobDescription != value)
                {
                    this.offer
                    .JobDescription = value;
                    OnPropertyChanged("JobDescription");
                }
            }
        }

        public CompanyViewModel Company
        {
            get
            {
                return new CompanyViewModel(this.offer.Company);
            }
            set
            {
                if (this.offer.Company == null)
                {
                    this.offer.Company = new CompanyDTO();
                }

                this.offer.Company.CompanyName = value.Name;
                OnPropertyChanged("Company");
            }
        }

        internal void Add()
        {
            try
            {
                JobsBoardCRUD.AddOffer(this.offer);
            }
            catch (Exception e)
            {
                MessageBox.Show("Update unseccesful");

                return;
            }
        }

        internal void Delete()
        {
            try
            {
                JobsBoardCRUD.DeleteOffer(this.offer);
            }
            catch
            {
                MessageBox.Show("Update unseccesful. Company refered by other object.");
                return;
            }
        }

        internal void Update()
        {
            try
            {
                JobsBoardCRUD.EditOffer(this.offer);
            }
            catch
            {
                MessageBox.Show("Update unseccesful");
                return;
            }
        }
    }
}